/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package com.unwiredgrain.modeling.sscm.impl;

import com.unwiredgrain.modeling.sscm.CppTemplateParameter;
import com.unwiredgrain.modeling.sscm.SSCMPackage;
import com.unwiredgrain.modeling.sscm.Template;
import com.unwiredgrain.modeling.sscm.TemplateParameter;

import java.util.Collection;

import org.eclipse.emf.common.notify.NotificationChain;

import org.eclipse.emf.common.util.EList;

import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.InternalEObject;

import org.eclipse.emf.ecore.util.EObjectContainmentEList;
import org.eclipse.emf.ecore.util.EcoreEList;
import org.eclipse.emf.ecore.util.InternalEList;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Template</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * The following features are implemented:
 * <ul>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.TemplateImpl#getTemplateParameters <em>Template Parameters</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.TemplateImpl#isTemplate <em>Template</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.TemplateImpl#getCppName <em>Cpp Name</em>}</li>
 *   <li>{@link com.unwiredgrain.modeling.sscm.impl.TemplateImpl#getCppTemplateParameters <em>Cpp Template Parameters</em>}</li>
 * </ul>
 * </p>
 *
 * @generated
 */
public abstract class TemplateImpl extends NamedElementImpl implements Template {
	/**
	 * The default value of the '{@link #isTemplate() <em>Template</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #isTemplate()
	 * @generated
	 * @ordered
	 */
	protected static final boolean TEMPLATE_EDEFAULT = false;

	/**
	 * The default value of the '{@link #getCppName() <em>Cpp Name</em>}' attribute.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getCppName()
	 * @generated
	 * @ordered
	 */
	protected static final String CPP_NAME_EDEFAULT = null;

	/**
	 * The cached value of the '{@link #getCppTemplateParameters() <em>Cpp Template Parameters</em>}' containment reference list.
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @see #getCppTemplateParameters()
	 * @generated
	 * @ordered
	 */
	protected EList<CppTemplateParameter> cppTemplateParameters;

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected TemplateImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return SSCMPackage.Literals.TEMPLATE;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public EList<TemplateParameter> getTemplateParameters() {
		// DONE: implement this method to return the 'Template Parameters' containment reference list
		// Ensure that you remove @generated or mark it @generated NOT
		// The list is expected to implement org.eclipse.emf.ecore.util.InternalEList and org.eclipse.emf.ecore.EStructuralFeature.Setting
		// so it's likely that an appropriate subclass of org.eclipse.emf.ecore.util.EcoreEList should be used.
		// throw new UnsupportedOperationException();
		// return new EcoreEList.UnmodifiableEList(this, , arg1, arg2, arg3) getCppTemplateParameters();
		return new EcoreEList.UnmodifiableEList<TemplateParameter>(this, 
																   SSCMPackage.Literals.TEMPLATE__TEMPLATE_PARAMETERS,
																   getCppTemplateParameters().size(),
																   getCppTemplateParameters().toArray());
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public boolean isTemplate() {
		// DONE: implement this method to return the 'Template' attribute
		// Ensure that you remove @generated or mark it @generated NOT
		// throw new UnsupportedOperationException();
		if (getTemplateParameters().isEmpty()) {
			return false;
		}
		
		return true;
	}

	/**
	 * <!-- begin-user-doc -->
	 * 
	 * return a cpp style name, such as classname<typename something, int anything>
	 * template cppname no greater_than concatenation problem.
	 * 
	 * <!-- end-user-doc -->
	 * @generated NOT
	 */
	public String getCppName() {
		// DONE: implement this method to return the 'Cpp Name' attribute
		// Ensure that you remove @generated or mark it @generated NOT
		// throw new UnsupportedOperationException();
		StringBuilder sb = new StringBuilder();
		
		if (getName() == null || getName().length() == 0) {
			sb.append("[UNSET]");
		}
		else {
			sb.append(getName());
		}
		
		if (!isTemplate()) {
			return sb.toString();
		}
		
		sb.append("<");
		
		int size = getTemplateParameters().size();
		for (int i = 0; i < size; i++) {
			sb.append(getTemplateParameters().get(i).getCppName());
			if (i != size - 1) { // not last one
				sb.append(", ");
			}
		}
		
		sb.append(">");
		
		return sb.toString();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	public EList<CppTemplateParameter> getCppTemplateParameters() {
		if (cppTemplateParameters == null) {
			cppTemplateParameters = new EObjectContainmentEList<CppTemplateParameter>(CppTemplateParameter.class, this, SSCMPackage.TEMPLATE__CPP_TEMPLATE_PARAMETERS);
		}
		return cppTemplateParameters;
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs) {
		switch (featureID) {
			case SSCMPackage.TEMPLATE__TEMPLATE_PARAMETERS:
				return ((InternalEList<?>)getTemplateParameters()).basicRemove(otherEnd, msgs);
			case SSCMPackage.TEMPLATE__CPP_TEMPLATE_PARAMETERS:
				return ((InternalEList<?>)getCppTemplateParameters()).basicRemove(otherEnd, msgs);
		}
		return super.eInverseRemove(otherEnd, featureID, msgs);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public Object eGet(int featureID, boolean resolve, boolean coreType) {
		switch (featureID) {
			case SSCMPackage.TEMPLATE__TEMPLATE_PARAMETERS:
				return getTemplateParameters();
			case SSCMPackage.TEMPLATE__TEMPLATE:
				return isTemplate();
			case SSCMPackage.TEMPLATE__CPP_NAME:
				return getCppName();
			case SSCMPackage.TEMPLATE__CPP_TEMPLATE_PARAMETERS:
				return getCppTemplateParameters();
		}
		return super.eGet(featureID, resolve, coreType);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@SuppressWarnings("unchecked")
	@Override
	public void eSet(int featureID, Object newValue) {
		switch (featureID) {
			case SSCMPackage.TEMPLATE__CPP_TEMPLATE_PARAMETERS:
				getCppTemplateParameters().clear();
				getCppTemplateParameters().addAll((Collection<? extends CppTemplateParameter>)newValue);
				return;
		}
		super.eSet(featureID, newValue);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public void eUnset(int featureID) {
		switch (featureID) {
			case SSCMPackage.TEMPLATE__CPP_TEMPLATE_PARAMETERS:
				getCppTemplateParameters().clear();
				return;
		}
		super.eUnset(featureID);
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	public boolean eIsSet(int featureID) {
		switch (featureID) {
			case SSCMPackage.TEMPLATE__TEMPLATE_PARAMETERS:
				return !getTemplateParameters().isEmpty();
			case SSCMPackage.TEMPLATE__TEMPLATE:
				return isTemplate() != TEMPLATE_EDEFAULT;
			case SSCMPackage.TEMPLATE__CPP_NAME:
				return CPP_NAME_EDEFAULT == null ? getCppName() != null : !CPP_NAME_EDEFAULT.equals(getCppName());
			case SSCMPackage.TEMPLATE__CPP_TEMPLATE_PARAMETERS:
				return cppTemplateParameters != null && !cppTemplateParameters.isEmpty();
		}
		return super.eIsSet(featureID);
	}

} //TemplateImpl
